<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色权限</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
    <link rel="stylesheet" href="../../../admin/css/reset.css"/>
</head>
<body>
<form class="layui-form" action="" lay-filter="form-data-filter">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label for="" class="layui-form-label">角色名称</label>
                <div class="layui-input-block">
                    <input type="text" id="role_name" maxlength="16" name="role_name" placeholder="" class="layui-input" readonly>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">权限</label>
                <div class="layui-input-block">
                    <ul id="checkbarTreeInitchoose" class="demo-tree" data-id="0"></ul>
                </div>
            </div>
        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>


<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../lms/base64.js"></script>
<script src="../../../lms/common.js"></script>
<script src="../../../lms/config.js"></script>
<script src="../../../lms/permission.js"></script>


<script>
    const PRIMARY_KEY = "role_id";
    const RESTFUL_API = lms_config.api_url + "/adminapi/setting/role_menu";
    const ROLE_RESTFUL_API = lms_config.api_url + "/adminapi/setting/role";
    const MENU_RESTFUL_API = lms_config.api_url + "/adminapi/setting/menu_list";

    if (!checkLogin()) {
        location.href = lms_config.base_path + "/login.html";
    }


    layui.use(['form', 'dtree', 'jquery', 'http'], function () {
        let form = layui.form;
        let Dtree = layui.dtree;
        let $ = layui.jquery;
        //场景处理
        if (getQueryString('operation') === 'edit') {
            get(getQueryString(PRIMARY_KEY), autoTree);
            layui.$("button[type='reset']").click(function () {
                location.reload();
            })
        }

        /**
         * 角色数据
         * @param id
         * @param callback
         */
        function get(id, callback) {
            $.ajax({
                url: ROLE_RESTFUL_API + '/' + getQueryString(PRIMARY_KEY),
                dataType: 'JSON',
                success: function (ret) {
                    if (ret.status == 200) {
                        let data = ret.data
                        form.val("form-data-filter", {
                            "role_code": data.role_code,
                            "role_name": data.role_name,
                        });
                        callback(data);
                    }
                }
            });
        }

        /**
         * 权限树
         * @param data
         */
        function autoTree(data) {
            $.ajax({
                url: MENU_RESTFUL_API,
                method: 'GET',
                dataType: 'JSON',
                success: function (ret) {
                    if (ret.status === 200) {
                        let tree_data = ret.data != undefined ? ret.data : [];
                        let level = data['level'] != undefined && parseInt(data['level']) === 0 ? 1 : 0;
                        appendCheckArr(tree_data, level)
                        Dtree.render({
                            elem: '#checkbarTreeInitchoose',
                            data: tree_data,
                            accordion: true,
                            checkbar: true,
                            checkbarType: "no-all",
                            checkbarData: "halfChoose",
                            skin: "layui",
                            response: {
                                treeId: "id", //节点ID（必填）
                                parentId: "pid", //父节点ID（必填）
                                title: "title", //节点名称（必填）
                            },
                            done: function (res, $ul, first) {
                                if (first && data.rules.length != 0 && level !== 1) {
                                    Dtree.chooseDataInit("checkbarTreeInitchoose", [data.rules]);
                                    Dtree.initNoAllCheck("checkbarTreeInitchoose");
                                }

                            }
                        });
                    }
                }
            });
        }

        /**
         * 表单提交
         */
        form.on('submit(save)', function (data) {
            let halfCheckedNodes = Dtree.getCheckbarNodesParam("checkbarTreeInitchoose", "half");
            data.field.checked_menus = array_column(halfCheckedNodes, 'nodeId');
            data.field.role_id = getQueryString(PRIMARY_KEY);
            $.ajax({
                url: RESTFUL_API,
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                dataType: 'json',
                success: function (ret) {
                    if (ret && ret.status == 200) {
                        layer.msg(ret.msg, {
                            icon: 1,
                            time: 1000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.refresh();
                        });
                        return false;
                    } else {
                        layer.msg(ret.msg, {
                            icon: 2,
                            time: 1000
                        });
                        return false;
                    }
                },
                error: function (ret) {
                    alert("出错" + ret.status + "：" + ret.responseText);
                },
            });
            return false;
        });
    })
</script>
</body>
</html>
